/**
 * Created by DELL on 2017/6/12.
 */
(function() {
    var checkedObject={};
    var settings = {
        "sAjaxSource": window.globalHost +common.sysParamConfListPath,
        "aoColumns": [{
            "mDataProp": function(obj) {
                var checkboxItemStr = '<input id="checkbox-' + obj.id + '" data-name="'+obj.contactName+'" type="checkbox" >';
                if (checkedObject[obj.id]) {
                    checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" checked="checked">';
                }
                return '<div class="checkbox checkbox-danger" style="width:100%">' + checkboxItemStr + '<label for="checkbox-' + obj.id + '" style="width:80%"></label></div>'
            },
            "sTitle": "",
            "sDefaultContent": ""
        },{
            "mDataProp": function (obj) {
                return '<a href="javascript:;" class="key" data-id="'+obj.id+'">'+obj.paramKey+'</a>'
            },
            "sTitle": "属性名",
            "sDefaultContent": ""
        }, {
            "mDataProp": function (obj) {
                return obj.paramValue.replace(common.regWarp,'<br/>')
            },
            "sTitle": "属性值",
            "sDefaultContent": ""
        }, {
            "mDataProp": "remark",
            "sTitle": "备注",
            "sDefaultContent": ""
        }],
        "fnServerParams": function(aoData) {
            return common.getSearchData("form.employeeSearch",aoData);
        }
    };
    var options = $.extend(true, {}, _dataTableSettings, settings);
    /**
     * 实例化公司员工列表插件
     * @type {[type]}
     */
    var $currentDataTable=$("#data-list");
    var oTable = $currentDataTable.dataTable(options);
    var createDialog=function (opt) {
        var sendData={};
        common.openTempDialog({
            type:1,
            title: '更新菜单信息',
            area:['900px', '700px'],
            content:opt.content,
            FailedMsg:"请完善菜单信息",
            sendData:sendData,
            ajaxUrl: opt.ajaxUrl || "",
            contentOnLoad:function (layerDocument) {
                // //弹窗内控件加载初始化
                if(opt.data){
                    var content=layerDocument.find("#menu-list-form");
                    common.initInputByNameOnLayer(opt.data,content);
                }
            },
            beforeSend:function (layerDocument) {
                var content=layerDocument.find("#menu-list-form");
                var json=common.formToJson(content);
                sendData=$.extend(sendData,json);
                return true;
            },
            resOk:function () {
                //如果更新成功，刷新表格数据
                oTable.fnClearTable();
            }
        });
    };
    var batchDelete=function () {
        var ids = [];
        if ($.isEmptyObject(checkedObject)) {
            layer.alert("请选择需要删除的参数 !");
            return;
        }
        $.each(checkedObject, function(i, I) {
            ids.push(i);
        });
        ids = ids.join();
        $.post(window.globalHost + common.deleteSysParamConfPath, { ids: ids }).success(function(data) {
            var record = common.transformData(data);
            layer.alert(record.res.msg);
            /**
             * 删除成功之后，清除本地的checkbox缓存。
             * @type {Object}
             */
            checkedObject = {};
            /**
             * 批量删除成功之后，刷新列表
             */
            oTable.fnClearTable();
        });
    };
    var subDom="";
    var upDom='<form style="margin-top: 10px;padding: 0 20px;" id="menu-list-form">'+
        '<input name="id" type="hidden">'+
        '<p  class="clearfix"><label class="col-xs-2">参数名</label><input class="col-xs-10" type="text" name="paramKey" required placeholder="参数名"></p>'+
        '<p  class="clearfix"><label class="col-xs-2">参数值</label><textarea class="col-xs-10" name="paramValue" style="height:150px;"  placeholder="参数值"></textarea></p>'+
        '<p  class="clearfix"><label class="col-xs-2">备注内容</label><textarea class="col-xs-10" name="remark" style="height:150px;"  placeholder="请输入备注内容"></textarea></p>'+
        '</form>';
    $currentDataTable.off("click",".key").on("click",".key",function (e) {
        var $this=$(this);
        var selfId=$this.attr("data-id");
        $.post(window.globalHost+common.sysParamConfInfoPath,{id:selfId}).success(function (data) {
            var detailInfo=common.transformData(data);
            createDialog({
                ajaxUrl:window.globalHost+common.updatesysParamConfPath,
                content:upDom,
                data:detailInfo
            })
        });
    });
    $currentDataTable.off("change", ".checkbox").on("change", ".checkbox", function(ev) {
        var checkItem = $(this).find("input[type='checkbox']")[0];
        var checkId = checkItem.id.replace('checkbox-', '');
        if (checkItem.checked) {
            checkedObject[checkId] = true;
        } else {
            delete checkedObject[checkId];
        }
        ev.stopPropagation();
    });
    $(".insertParam").off("click").on("click",function (e) {
        createDialog({
            ajaxUrl:window.globalHost+common.addSysParamConfPath,
            content:upDom
        });
    });
    $(".deleteParam").off("click").on("click",function (e) {
        common.changeConfirm("确定删除以上参数吗？",batchDelete);
        e.stopPropagation();
    });
}).call(this);

